﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HRMS_Model;
using System.Diagnostics;

namespace HRMS.Models
{
    public class DesignationsModels
    {
        public static bool AddDesignation(Designation designation)
        {
            try
            {
                using (var desigEntity = new HRMSEntities())
                {
                    desigEntity.Designations.AddObject(designation);
                    desigEntity.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }
        public static bool DeleteDesignation(int id)
        {
            try
            {
                using (var designationEntity = new HRMSEntities())
                {
                    designationEntity.Designations.DeleteObject(designationEntity.Designations.First(d => d.DesignationId == id));
                    designationEntity.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }
        public static List<Designation> GetDesignations(string sortExpression, string sortDirection, int pageIndex, int pageSize, int pageCount)
        {
            using (var designationEntity = new HRMSEntities())
            {
                return designationEntity.Designations.OrderBy("it." + sortExpression + " " + sortDirection).Skip(pageIndex * pageSize).Take(pageCount * pageSize).ToList();
            }
        }

        public static int GetDesignationsCount()
        {
            using (var designationEntity = new HRMSEntities())
            {
                return designationEntity.Designations.Count();
            }
        }

        public static bool UpdateDesignation(Designation designation)
        {
            try
            {
                using (var designationEntity = new HRMSEntities())
                {
                    var designationToEdit = designationEntity.Designations.FirstOrDefault(d => d.DesignationId == designation.DesignationId);
                    if (designationToEdit != null) designationToEdit.DesignationName = designation.DesignationName;
                    designationEntity.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }
    }
}